package servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import beans.ViewChangeBean;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * Takes the paramters from, to and text and compiles a message which is sent
 * 
 * @author jonas
 */
public class MailServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        String host = "mail.chalmers.se";

        HttpSession session = request.getSession();
        ViewChangeBean vcb = (ViewChangeBean)session.getAttribute(ViewChangeBean.MANGED_BEAN_NAME);

        String frtoStr = vcb.getParam();
        String[] splitStrArr = frtoStr.split("fr=|to=|text=");
        if(splitStrArr.length != 4){
            request.getRequestDispatcher("/secure/mailTo.jspx?mailSent=0").forward(request, response);
            vcb.setParam("No address.");
            return;
        }

        String from = splitStrArr[1];
        String to = splitStrArr[2];
        String mailText = splitStrArr[3];

        // Get system properties
        Properties props = System.getProperties();

        // Setup mail server
        props.put("mail.smpt.host", "smtp.gmail.com");
        props.put("mail.smtp.user", "studentportaldistapp");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.starttls.enable", true);

        //props.put("mail.smtp.host", host);
        // Get MAIL session
        Session mailSession = Session.getDefaultInstance(props, null);
        // Define message
        MimeMessage message = new MimeMessage(mailSession);

        boolean msgSent = true;

        try {
            InternetAddress aFrom = new InternetAddress(from);
            //InternetAddress aTo = new InternetAddress(to);
            InternetAddress aTo = new InternetAddress("bompa85@gmail.com");
            message.setFrom(aFrom);
            message.addRecipient(Message.RecipientType.TO, aTo);
            message.setSubject("Student portal message");
            message.setText(mailText);

            Transport tr = mailSession.getTransport("smtp");
            tr.connect("smtp.gmail.com", 587, "studentportaldistapp", "u9ThnaNj");
            message.saveChanges();
            
            tr.sendMessage(message, message.getAllRecipients());
            tr.close();
        } catch (AddressException ex) {
            ex.printStackTrace();
            msgSent = false;
        } catch (MessagingException ex) {
            ex.printStackTrace();
            msgSent = false;
        }

        vcb.setParam(to);
        if(msgSent){
            response.sendRedirect("./mailTo.jspx?mailSent=1");
        }else{
            response.sendRedirect("./mailTo.jspx?mailSent=0");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
